package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.entity.TTran;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.query.TranQuery;
import com.bjpowernode.service.TTranService;
import com.bjpowernode.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TTranServiceImpl implements TTranService {

    @Resource
    private TTranMapper tTranMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private RedisManager redisManager;

    @Override
    public int createTran(TranQuery tranQuery) {
        // 创建对象
        TTran tTran = new TTran();
        // 复制转换
        BeanUtils.copyProperties(tranQuery, tTran);
        // 设置tran_no
        tTran.setTranNo(System.currentTimeMillis() + "" + redisManager.getOnlyNumber());
        String parseToken = jwtUtil.parseToken(tranQuery.getJwt());
        TUser tUser = JSONUtil.toBean(parseToken, TUser.class);
        tTran.setCreateBy(tUser.getId());
        tTran.setCreateTime(DateTime.now());
        tTran.setEditBy(tUser.getId());
        tTran.setEditTime(DateTime.now());
        return tTranMapper.insertSelective(tTran);
    }
}
